package com.msmall.community.klg.preparer;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.Constants;
import com.msmall.community.common.model.Commend;

public class IndexCommendPreparer implements ViewPreparer{
	private static final Log log = LogFactory.getLog(IndexCommendPreparer.class);
	
	@SuppressWarnings("unchecked")
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			Long segid = (Long) context.getAttribute("segid").getValue();
			Map<Long,Commend> klgCommendImages = (Map<Long,Commend>)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KLG_COMMEND_IMAGES);
			Commend commend = null;
			if(klgCommendImages != null){
				commend = klgCommendImages.get(segid);
			}
			Map<Long, List<Commend>> klgCommendTopNews = (Map<Long, List<Commend>>)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KLG_COMMEND_TOP_NEWS);
			List<Commend> commends = null;
			if(klgCommendTopNews != null){
				commends = klgCommendTopNews.get(segid);
			}
			
			request.setAttribute("indexKlgImage", commend);
			request.setAttribute("indexKlgTopNews", commends);
		}catch(Exception e){
			log.debug(e);
		}
	}
}
